package com.sky.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    public static String getDate() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(d);
        return format;
    }

    public static String getNowTime() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String format = sdf.format(d);
        return format;
    }

    public static String getNowDate() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(d);
        return format;
    }


    public static String getTomorrow() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    // 新增的
    public static String getYesterday() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 时间格式转成字符串格式
     **/
    public static String getString(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        return format;
    }

    /**
     * 字符串转成时间格式
     **/
    public static Date getDate(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date strDate = sdf.parse(date);
        return strDate;
    }

    /**
     * 比较一个时间是否在一个时间段内，返回1代表在，返回0代表不在，仅仅是字符时间，不包括日期 parameter value
     * 需要比较的时间；08:00:00 parameter sTime 开始时间；06:00:00 parameter eTime
     * 结束时间；20:00:00 return 1在时间内，0 不在
     **/
    public static int compareTime(String value, String sTime, String eTime) {
        // 全部转换成秒比较
        int newValue = Integer.parseInt(value.substring(0, 2)) * 3600 + Integer.parseInt(value.substring(3, 5)) * 60
                + Integer.parseInt(value.substring(6, 8));
        int newStime = Integer.parseInt(sTime.substring(0, 2)) * 3600 + Integer.parseInt(sTime.substring(3, 5)) * 60
                + Integer.parseInt(sTime.substring(6, 8));
        int newEtime = Integer.parseInt(eTime.substring(0, 2)) * 3600 + Integer.parseInt(eTime.substring(3, 5)) * 60
                + Integer.parseInt(eTime.substring(6, 8));
        if (newStime < newValue) {
            System.out.println("输出时间值：newValue-newStime相差秒数");
            System.out.println(newValue - newStime);
            if (newValue < newEtime) {
                System.out.println("输出时间值：newEtime-newValue相差秒数");
                System.out.println(newEtime - newValue);
                return 1;
            }
        }
        return 0;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime   当前时间
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @returngetDate
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        return nowTime.getTime() >= beginTime.getTime() && nowTime.getTime() <= endTime.getTime();
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(getBeforeIntervalDays("2022-01-03"));
    }

    public static int singleNumber(int[] nums) {
        int num = 0;
        for (int i = 0; i < nums.length; i++) {
                /*
                涉及到异或运算，对于这道题，你应该了解异或有什么样性质
                1. 任何数与0异或，都为原数
                2. 与本身自己异或，则为0
                3. 交换律和结合律（解决这道题的关键），你把整个循环的计算写成数学运算过程，你会发现要利用交换律
                结合律，可以实现这道题的要求
                 */
            num ^= nums[i];
        }
        return num;
    }

    /**
     * 获取传入时间的前一天
     **/
    public static String getYesterday(String date) {
        String year = date.substring(0, 4);
        String month = date.substring(5, 7);
        String day = date.substring(8, 10);
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        String yesterDay = "";
        if (day.equals("01")) {
            if (month.equals("01")) {
                //年数减1
                year = String.valueOf((Integer.parseInt(year) - 1));
                month = "12";
                day = "31";
                yesterDay = year + "-" + month + "-" + day;
                return yesterDay;
            } else {
                //月数减1
                month = String.valueOf((Integer.parseInt(month) - 1));
                if (month.length() == 1) {
                    month = "0" + month;
                }
                if (month.equals("02")) {
                    System.out.println(Integer.parseInt(year) % 4);
                    if (Integer.parseInt(year) % 4 == 0) {
                        //闰年
                        day = "29";
                    } else {
                        //平年
                        day = "28";

                    }
                } else if (month.equals("01") || month.equals("03") || month.equals("05")
                        || month.equals("07") || month.equals("08") || month.equals("10")
                        || month.equals("12")) {
                    day = "31";
                } else {
                    day = "30";
                }
                yesterDay = year + "-" + month + "-" + day;
                return yesterDay;
            }
        } else {
            //日数减1
            day = String.valueOf((Integer.parseInt(day) - 1));
            if (day.length() == 1) {
                day = "0" + day;
            }
            yesterDay = year + "-" + month + "-" + day;
        }

        return yesterDay;
    }

    //把天数转成天数+小时+分钟
    public static String formatDay(BigDecimal dayNum) {
        //把值转成分钟
        BigDecimal value = new BigDecimal("1440");
        BigDecimal minute = dayNum.multiply(value);
        System.out.println(minute);
        //忽略分钟后面的值
        int intzMinute = minute.intValue();
        System.out.println(intzMinute);
        //求余获取分钟
        int intMinute = intzMinute % 60;
        System.out.println(intMinute);
        //减去分钟
        int a = intzMinute - intMinute;
        //获取小时
        int intHour = (a / 60) % 24;
        System.out.println(intHour);
        int intDay = (intzMinute - intMinute - intHour) / 1440;
        System.out.println(intDay);
        return intDay + "天" + intHour + "时" + intMinute + "分";
    }

    //获取传入时间距离当前时间的天数
    // TODO: 2020/12/3 方法待检验 
    public static Integer getDays(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (sdf.parse(date).before(new Date())) {
            return 0;
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(new Date());
        cReturnDate.setTime(sdf.parse(date));
        setTimeToMidnight(cNow);
        setTimeToMidnight(cReturnDate);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = todayMs - returnMs;
        return millisecondsToDays(intervalMs);
    }

    public static Integer getBeforDays(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (!sdf.parse(date).before(new Date())) {
            return 0;
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(new Date());
        cReturnDate.setTime(sdf.parse(date));
        setTimeToMidnight(cNow);
        setTimeToMidnight(cReturnDate);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = todayMs - returnMs;
        return millisecondsToDays(intervalMs);
    }

    //获取传入时间距离当前时间的天数
    public static Integer getIntervalDays(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!sdf.parse(date).before(new Date())) {
            return 0;
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(new Date());
        cReturnDate.setTime(sdf.parse(date));
        setTimeToMidnight(cNow);
        setTimeToMidnight(cReturnDate);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = todayMs - returnMs;
        return millisecondsToDays(intervalMs);
    }

    //获取当前时间距离传入时间的天数
    public static Integer getBeforeIntervalDays(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (sdf.parse(date).before(new Date())) {
            return 0;
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(new Date());
        cReturnDate.setTime(sdf.parse(date));
        setTimeToMidnight(cNow);
        setTimeToMidnight(cReturnDate);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = returnMs - todayMs;
        return millisecondsToDays(intervalMs);
    }

    //获取两个日期之间的分钟数
    private static int millisecondsToDays(long intervalMs) {
        return (int) (intervalMs / (1000 * 86400));
    }

    //获取两个日期之间的毫秒数
    private static void setTimeToMidnight(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
    }

    //时间推前2个月
    public static Date datePushTwoMonth(String oldDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date newDate = null;
        try {
            newDate = sdf.parse(oldDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(newDate);
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

}
